/** 
 * 解析来自命令行的参数
 * 示例： npm run build -- -user=dongls
 * 注意 -- 两侧存在空格 
 * 
 * --user, -u 用于名
 * --env, -e 运行环境
 * --runType, -r 运行类型
 * --cloud, -c 服务端端环境
 * 
 * @author dongls 
 */
const minimist = require('minimist');

module.exports = function(argv){
  
  let args = minimist(argv);
  
  let envData = {}
  
  try {
    
    const allowBranch = args.allowBranch || process.env.npm_config_allowBranch || process.env.npm_config_allowbranch
    const notAllowBranch = args.notAllowBranch || process.env.npm_config_notAllowBranch || process.env.npm_config_notallowbranch
    const noCache = Boolean(args.noCache || process.env.npm_config_noCache || process.env.npm_config_nocache)
    const resourcePath = args.resourcePath || process.env.npm_config_resourcePath || process.env.npm_config_resourcepath
    
    envData = {
      allowBranch,
      notAllowBranch,
      noCache,
      resourcePath
    }
    
  } catch (error) {
    console.log(error);
  }
  
  return {
    user: args.user || args.u,
    env: args.env || args.e,
    runType: args.runType || args.r,
    cloud: args.cloud || args.c,
    sameBranch: args.sameBranch || args.sb,
    immediately: args.immediately || args.i,
    analyzer: args.analyzer, // 是否打包分析
    // 是否使用vite打包
    vite: args.vite || args.v,
    allowBranch: envData.allowBranch,
    notAllowBranch: envData.notAllowBranch,
    noCache: envData.noCache,
    resourcePath: envData.resourcePath,
    _: args._
  }
}